﻿//基本表单	<div FORM="YYY.user" id="XXX"><input name="name1" /><input name="name2" /></div>
//fastCSharp.Forms.XXX.SubmitName='name2';
//fastCSharp.Forms.XXX.OnSubmit.Add(AAA);
//fastCSharp.Forms.XXX.Start({name1:{MaxLength:32,OnVerify:/^[0-9A-Z_\.]+@[0-9A-Z_\.]+$/gi,ErrorInfo:'错误'},name2:{...}});
//fastCSharp.Forms.XXX.GetValue();
(function(){
var Form=function()
	{
	this.GetArgument(arguments[0],{Id:null,SubmitName:null});
	this.OnSubmit=fastCSharp.Event();
	this.OnError=fastCSharp.Event();
	};
(fastCSharp.Functions.Form=Form).Inherit(fastCSharp.BaseFunction,{
TagNames:{
	input:{hidden:1,text:1,password:1,checkbox:3,radio:5},
	select:2,
	textarea:4
	},
ElementType:function(Element)
	{
	var Value=this.TagNames[Element.tagName.toLowerCase()];
	if(Value&&typeof(Value)!='number')	Value=Value[Element.type.toLowerCase()];
	return Value||0;
	},
IsElement:function(Element)
	{
	return Element.name?this.ElementType(Element):0;
	},
GetElements:function()
	{
	return this.$Id(this.Id).Childs(fastCSharp.ThisFunction(this,this.IsElement)).GetElements();
	},
Start:function(Elements,IsFocus)
	{
	this.Elements=Elements||{};
	for(var Elements=this.GetElements(),Index=-1;++Index-Elements.length;)
		{
		var Element=Elements[Index];
		if(Element.readOnly)
			{
			if(this.Elements[Element.name])	this.Elements[Element.name].NullValue=Element.value.Trim();
			else	this.Elements[Element.name]={NullValue:Element.value.Trim()};
			Element.readOnly=false;
			}
		this.$AddEvent(Element,['keydown'],fastCSharp.ThisEvent(this,this.KeyDown));
		var Type=this.ElementType(Element);
		if(Type==1||Type==4)
			{
			var FormElement=this.Elements[Element.name];
			if(FormElement&&FormElement.MaxLength)	Element.maxLength=FormElement.MaxLength;
			this.$AddEvent(Element,['blur'],fastCSharp.ThisEvent(this,this.Format));
			this.$AddEvent(Element,['focus'],fastCSharp.ThisEvent(this,this.Focus));
			}
		}
	this.$Name('submit',this.$Id(this.Id).Element()).Cursor('pointer').AddEvent('click',fastCSharp.ThisEvent(this,this.Submit));
	this.Disabled(false);
	this.Cancel(false);
	if(IsFocus==null||IsFocus)
		{
		try	{	Elements[0].focus();	}
		catch(e){}
		}
	},
Submit:function()
	{
	var Value=this.GetValue();
	if(Value)	this.OnSubmit(Value);
	},
Cancel:function(IsReset,IsOnCancel)
	{
	for(var Elements=this.GetElements(),Index=-1;++Index-Elements.length;)
		{
		var Element=Elements[Index],FormElement=this.Elements[Element.name],Value=FormElement?(FormElement.DefaultValue!=null?FormElement.DefaultValue:FormElement.NullValue):null;
		if(Value!=null)
			{
			var Type=this.ElementType(Element);
			if(Type==1||Type==4)
				{
				if(Element.value==''||IsReset==null||IsReset)	Element.value=Value?Value:'';
				}
			else if(Type==2)
				{
				if(Element.options!='')
					{
					if(Element.options[0].value!='')
						{
						for(Value=Element.options.length-1;Value>0&&Element.options[Value].value!=FormElement.DefaultValue;Value--);
						}
					Element.selectedIndex=Value;
					}
				}
			else if(Type==3)	Element.checked=Value?Value:false;
			else if(Type==5)	this.$(this.$Name(Element.name,this.$Id(this.Id).Element()).First(function(Element){return Element.value==Value;})).Set('checked',true);
			if(FormElement.OnChange)	FormElement.OnChange();
			}
		};
	if((IsOnCancel==null||IsOnCancel)&&this.OnCancel)	this.OnCancel();	
	},
KeyDown:function(Event)
	{
	var Key=Event.shiftKey||Event.keyCode,Element=Event.srcElement,Type=this.ElementType(Element),Next;
	if(Type==1||Type==3||Type==5)
		{
		if(Type==3||Type==5)
			{
			if(Key==13)	Element.click();
			else if(Key==39||Key==40)	Next=1;
			else if(Key==37||Key==38)	Next=-1;
			}
		else if(Key==40)	Next=1;
		else if(Key==38)	Next=-1;
		else if(Key==13)
			{
			if(Element.name==this.SubmitName)
				{
				Event.CancelBubble();
				this.Submit();
				}
			else	Next=1;
			}
		}
	else if(Type==2&&Key==13) Next=1;
	if(Next){
		for(var Elements=this.GetElements(),Index=-1;++Index-Elements.length;)
			{
			if(Elements[Index]==Element)
				{
				if((Index+=Next)>=0&&Index<Elements.length)	Elements[Index].focus();
				break;
				}
			}
		Event.CancelBubble();
		}
	},
Format:function(Event)
	{
	var Element=Event.srcElement,FormElement=this.Elements[Element.name],Type=this.ElementType(Element);
	if(Type==1||Type==4)	Element.value=Element.value.replace(/[\x00]/g,'');
	if(FormElement)
		{
		if(FormElement.OnFormat)
			{
			var Value=FormElement.OnFormat(this.GetElementValue(Element)).toString();
			if(Type==1||Type==4)	Element.value=Value==''?(FormElement.NullValue||''):Value;
			else if(Type!=2&&Type!=3&&Type!=5)	Element.innerHTML=Value;
			}
		if(FormElement.NullValue&&(Type==1||Type==4)&&(!Element.value))	Element.value=FormElement.NullValue;
		if(Type==4&&FormElement.MaxLength&&FormElement.MaxLength&&Element.value!=FormElement.NullValue&&Element.value>FormElement.MaxLength)
			{
			Element.value=Element.value.substring(0,FormElement.MaxLength);
			}
		}
	},
Focus:function(Event)
	{
	var Element=Event.srcElement,FormElement=this.Elements[Element.name];
	if(FormElement&&FormElement.NullValue)
		{
		var Value=Element.value.Trim();
		if(Value==FormElement.NullValue)	Element.value='';
		else if(Value=='')	Element.value=FormElement.NullValue;
		}
	Element.select();
	},
GetElementValue:function(Element)
	{
	var Type=this.ElementType(Element);
	if(Type==1||Type==4)
		{
		var FormElement=this.Elements[Element.name];
		return FormElement&&Element.value.Trim()==FormElement.NullValue?'':Element.value;
		}
	if(Type==3)	return Element.checked;
	if(Type==5)	return this.$Name(Element.name,this.$Id(this.Id).Element()).First(function(Element){return Element.checked}).value;
	if(Type==2)	return Element.options[Element.selectedIndex].value?Element.options[Element.selectedIndex].value:null;
	return Element.innerHTML;
	},
GetValue:function(Name)
	{
	if(Name){
		for(var Elements=this.GetElements(),Index=-1;++Index-Elements.length;)
			{
			var Element=Elements[Index];
			if(Element.name==Name)	return this.GetElementValue(Element);
			}
		}
	else	{
		for(var ReturnValue={},Elements=this.GetElements(),Index=-1;++Index-Elements.length;)
			{
			var Element=Elements[Index],FormElement=this.Elements[Element.name],Value=this.GetElementValue(Element),OnVerify=FormElement?FormElement.OnVerify:null;
			if(OnVerify&&(!(typeof(OnVerify)=='function'&&OnVerify.toString().charAt(0)!='/'?OnVerify(Value):Value.toString().match(OnVerify))))
				{
				ReturnValue=null;
				Element.focus();
				if(this.OnError.Get().length)	this.OnError(FormElement);
				else if(FormElement.ErrorInfo)	alert(FormElement.ErrorInfo);
				break;
				}
			ReturnValue[Element.name]=Value;
			}
		return ReturnValue;
		}
	},
Disabled:function(IsDisabled)
	{
	var Cursor=(this.IsDisabled=(IsDisabled==null||IsDisabled))?'auto':'pointer';
	for(var Elements=this.GetElements(),Index=Elements.length;--Index>=0;)	Elements[Index].disabled=this.IsDisabled;
	this.$Name('submit',this.$Id(this.Id).Element()).Disabled(this.IsDisabled).Cursor(Cursor);
	}
		});
fastCSharp.Forms=function()
	{
	this.Forms={};
	for(var Childs=this.$('@FORM').GetElements(),Index=Childs.length;--Index>=0;this.Forms[Childs[Index].id]=new this.Functions.Form({Id:Childs[Index].id}));
	};
fastCSharp.OnLoad(fastCSharp.Forms,fastCSharp,1);
})();